package com.atguigu.dao.impl;

import com.atguigu.bean.Book;
import com.atguigu.bean.Page;
import com.atguigu.dao.BaseDao;
import com.atguigu.dao.BookDao;

import java.util.List;

/**
 * Date:2020/9/11
 * Author:ybc
 * Description:
 */
public class BookDaoImpl extends BaseDao<Book> implements BookDao {
    @Override
    public List<Book> getBookPage(Page<Book> page) {
        String sql = "select * from bs_book limit ?,?";
        return getBeanList(Book.class, sql, page.getIndex(), page.getPageSize());
    }

    @Override
    public Integer getCount() {
        String sql = "select count(*) from bs_book";
        Long singleData = (Long) getSingleData(sql);
        return  singleData.intValue();
    }

    @Override
    public List<Book> getBookList() {
        String sql = "select * from bs_book";
        return getBeanList(Book.class, sql);
    }

    @Override
    public void addBook(Book book) {
        String sql = "insert into bs_book values(null,?,?,?,?,?,?)";
        update(sql, book.getTitle(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImgPath());
    }

    @Override
    public void deleteBook(Integer id) {
        String sql = "delete from bs_book where id = ?";
        update(sql, id);
    }

    @Override
    public Book getBookByBookId(Integer id) {
        String sql = "select * from bs_book where id = ?";
        return getBean(Book.class, sql, id);
    }

    @Override
    public void updateBook(Book book) {
        String sql = "update bs_book set title = ?, author = ?, price = ?, sales = ?, stock = ?, imgPath = ? where id = ?";
        update(sql, book.getTitle(), book.getAuthor(), book.getPrice(), book.getSales(), book.getStock(), book.getImgPath(), book.getId());
    }
}
